//
//  MineViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/13.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "MineViewController.h"
#import "LoginViewController.h"
#import "MineMessageViewController.h"
#import "MineSettingViewController.h"
#import "MineWalletViewController.h"
#import "MineCollectViewController.h"
#import "MineOrderViewController.h"
#import "MineScoreViewController.h"
#import "MineProfitViewController.h"
#import "MineAddressViewController.h"
#import "MineStoreViewController.h"
#import "MineAwardViewController.h"
#import "MineShareViewController.h"
#import "CreateQrcodeImage+Utils.h"
#import "PersonalSettingViewController.h"
#import "MineLevelViewController.h"

@interface MineViewController ()<WKNavigationDelegate,WKScriptMessageHandler>
{
    UIView *_BackgroundView;
    UIImageView *_QRCodeBack;
    UIImageView *_QRCode;
}
@property (strong, nonatomic) WKWebView *webView;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc]init];
    //注册js方法
    config.userContentController = [[WKUserContentController alloc]init];
    _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT-20) configuration:config];
    _webView.navigationDelegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?userToken=%@", API(MineHome), [Utils getUserToken]]]]];
    [self.view addSubview:_webView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RefershWebView) name:@"LoginSuccess" object:nil];
}

- (void)RefershWebView
{
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?userToken=%@", API(MineHome), [Utils getUserToken]]]]];
}

// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [self.view loadingOnAnyView];
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self.view removeAnyView];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    [super viewWillAppear:animated];
    [_webView.configuration.userContentController addScriptMessageHandler:self name:@"my"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(RefershWebView) name:@"ChangUI" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_webView.configuration.userContentController removeScriptMessageHandlerForName:@"my"];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ChangUI" object:nil];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.body isEqualToString:@"msg"]) {
        //消息
        MineMessageViewController *msg = [[MineMessageViewController alloc] init];
        msg.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:msg animated:YES];
    }else if ([message.body isEqualToString:@"set"]) {
        //设置
        MineSettingViewController *set = [[MineSettingViewController alloc] init];
        set.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:set animated:YES];
    }else if ([message.body isEqualToString:@"img"]) {
        //换头像
        PersonalSettingViewController *set = [[PersonalSettingViewController alloc] init];
        set.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:set animated:YES];
    }else if ([message.body isEqualToString:@"my1"]) {
        //我的奖励
        MineAwardViewController *refund = [[MineAwardViewController alloc] init];
        refund.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:refund animated:YES];
    }else if ([message.body isEqualToString:@"my2"]) {
        //我的余额
        MineWalletViewController *wallet = [[MineWalletViewController alloc] init];
        wallet.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:wallet animated:YES];
    }else if ([message.body isEqualToString:@"my3"]) {
        //我的分润点
        MineProfitViewController *profit = [[MineProfitViewController alloc] init];
        profit.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:profit animated:YES];
    }else if ([message.body isEqualToString:@"my4"]) {
        //我的订单
        MineOrderViewController *order = [[MineOrderViewController alloc] init];
        order.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:order animated:YES];
    }else if ([message.body isEqualToString:@"my5"]) {
        //我的收藏
        MineCollectViewController *collect = [[MineCollectViewController alloc]init];
        collect.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:collect animated:YES];
    }else if ([message.body isEqualToString:@"my6"]) {
        //店铺管理
        MineStoreViewController *store = [[MineStoreViewController alloc] init];
        store.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:store animated:YES];
    }else if ([message.body isEqualToString:@"my7"]) {
        //分享好友
        MineShareViewController *share = [[MineShareViewController alloc] init];
        share.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:share animated:YES];
    }else if ([message.body isEqualToString:@"my8"]) {
        //我的注册码
        [THWManager PostHttp:API(MineEqcode) Parameters:@{@"userToken":[Utils getUserToken]} Success:^(id responseObject) {
            if ([String(responseObject[@"status_code"]) isEqualToString:@"1008"]) {
                [self.view Message:responseObject[@"msg"]];
            }else{
                if (_QRCodeBack) {
                    _BackgroundView.hidden = NO;
                    _QRCodeBack.hidden = NO;
                }else{
                    _BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT-20)];
                    _BackgroundView.backgroundColor = [UIColor grayColor];
                    _BackgroundView.alpha = 0.6f;
                    [self.view addSubview:_BackgroundView];
                    
                    _QRCodeBack = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH-(190*WIDTH/280))/2, (HEIGHT-(260*HEIGHT/475))/2, 190*WIDTH/280, 260*HEIGHT/475)];
                    _QRCodeBack.userInteractionEnabled = YES;
                    [_QRCodeBack setImage:[UIImage imageNamed:@"QRCodeBackground"]];
                    [self.view addSubview:_QRCodeBack];
                    
                    _QRCode = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 190*WIDTH/280-40, 190*WIDTH/280-40)];
                    
                    NSString *qrcodStr = [CreateQrcodeImage_Utils CreateQrcodeString:responseObject[@"data"][@"url"]];
                    CIImage *CIImage = [CreateQrcodeImage_Utils createErweimaWithString:qrcodStr];
                    _QRCode.image = [CreateQrcodeImage_Utils createNonInterpolatedUIImageFormCIImage:CIImage withSize:200.0f];
                    [_QRCodeBack addSubview:_QRCode];
                    
                    UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 260*HEIGHT/475-40, 190*WIDTH/280, 20)];
                    codeLabel.text = [NSString stringWithFormat:@"ID:%@", responseObject[@"data"][@"id"]];
                    codeLabel.textColor = [UIColor whiteColor];
                    codeLabel.textAlignment = NSTextAlignmentCenter;
                    [_QRCodeBack addSubview:codeLabel];
                    
                    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                    closeBtn.frame = CGRectMake(190*WIDTH/280-30, 0, 30, 30);
                    [closeBtn setTitle:@"X" forState:UIControlStateNormal];
                    [closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
                    [_QRCodeBack addSubview:closeBtn];
                }
                
            }
        } andFalse:^(NSError *error) {
            
        }];
    }else if ([message.body isEqualToString:@"my9"]) {
        //我的地址
        MineAddressViewController *address = [[MineAddressViewController alloc] init];
        address.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:address animated:YES];
    }else if ([message.body isEqualToString:@"my10"]) {
        //我的信用分
        MineScoreViewController *score = [[MineScoreViewController alloc] init];
        score.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:score animated:YES];
    }else if ([message.body isEqualToString:@"my11"]) {
        //职称管理
        MineLevelViewController *userName = [[MineLevelViewController alloc] init];
        userName.hidesBottomBarWhenPushed = YES;
        userName.type = @"user";
        [self.navigationController pushViewController:userName animated:YES];
    }else if ([message.body isEqualToString:@"my12"]) {
        //等级管理
        MineLevelViewController *level = [[MineLevelViewController alloc] init];
        level.hidesBottomBarWhenPushed = YES;
        level.type = @"level";
        [self.navigationController pushViewController:level animated:YES];
    }else if ([message.body isEqualToString:@"login"]) {
        //登录
        LoginViewController *login = [[LoginViewController alloc] init];
        login.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:login animated:YES];
    }
}

- (void)closeClick
{
    _BackgroundView.hidden = YES;
    _QRCodeBack.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
